<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Process extends Controller {
	function Process()
	{
		parent::Controller();	
		$this->load->model('user_model');
	}

	function index()
	{
	    if ($_SESSION['logged_in_status'] == FALSE)
	    {
			redirect('/admin/');
	    }
		else
		{
			//var_dump($_POST);
			//set_time_limit(200);
			$arr=$_POST;
			switch ($arr['component']) 
			{
				case "com_config":
					$this->com_config($arr);
					break;
				case "com_category":
					$this->com_category($arr);
					break;					
				case "com_reklame_customer":
					$this->com_reklame_customer($arr,$_FILES['userfile']['name']);
					break;				
				case "com_users":
					$this->com_users($arr);
					break;
				case "com_article":
					$this->com_article($arr,$_FILES['userfile']['name']);
					break;
				case "com_product":
					$this->com_product($arr,$_FILES['userfile']['name']);
					break;
				case "com_information":
					$this->com_information($arr);
					break;
				case "com_competition":
					$this->com_competition($arr);
					break;
				case "com_link":
					$this->com_link($arr);
					break;					
				case "com_faq":
					$this->com_faq($arr);
					break;
				case "com_human_org":
					$this->com_human_org($arr,$_FILES['userfile']['name']);
					break;								
				default:
					echo "You can't access this";
					break;
			}
		}
	}
	
	function com_competition($arr)
	{
		//var_dump($arr);
			switch ($arr['option']) 
			{
				case "add_competition_item":
					$this->add_competition_item($arr);
					break;
				case "add_pssst_item":
					$this->add_pssst_item($arr);
					break;
				case "update_competition_item":
					$this->update_competition_item($arr);
					break;
				case "update_pssst_item":
					$this->update_pssst_item($arr);
					break;
				default:
					echo "You can't access thisg sdgsd";
					break;
			}

	}

	function com_information($arr)
	{
		//var_dump($arr);
			switch ($arr['option']) 
			{
				case "update_information":
					$this->update_information($arr);
					break;
				default:
					echo "You can't access thisg sdgsd";
					break;
			}

	}
	
	function com_faq($arr)
	{
		//var_dump($arr);
			switch ($arr['option']) 
			{
				case "add_faq_item":
					$this->add_faq_item($arr);
					break;
				case "update_faq_item":
					$this->update_faq_item($arr);
					break;
				case "add_review_item":
					$this->add_review_item($arr);
					break;
				case "update_review_item":
					$this->update_review_item($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}

	}
	function com_article($arr,$fileimage)
	{
	
			switch ($arr['option']) 
			{
				case "add_article":
					$this->add_article($arr);
					break;
				case "update_article":
					$this->update_article($arr,$fileimage);
					break;
				case "search_article":
					$this->search_article($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function com_reklame_customer($arr,$fileimage)
	{
			switch ($arr['option']) 
			{
				case "add_reklame_customer_item":
					$this->add_reklame_customer_item($arr);
					break;
				case "update_article":
					$this->update_article($arr,$fileimage);
					break;
				default:
					echo "You can't access this";
					break;
			}
	}
	function add_reklame_customer_item($arr)
	{
		//print_r($arr);
		if ($this->general_model->add_reklame_customer_item($arr)==1)
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else if($this->general_model->add_reklame_customer_item($arr)==-1)
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">Filesize is bigger than allowed, please try again.</div>');
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}


	}
	function com_human_org($arr,$fileimage)
	{
		//echo "<br>".$arr['option']."/".$arr['task'];
		
			switch ($arr['option']) 
			{
				case "add_human_org_item":
					$this->add_human_org_item($arr);
					break;
				case "update_product":
					$this->update_product($arr,$fileimage);
					break;
				case "search_product":
					$this->search_product($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function add_competition_item($arr)
	{
		if ($this->general_model->add_competition_item($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	function add_pssst_item($arr)
	{
		if ($this->general_model->add_pssst_item($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	function update_information($arr)
	{
		if ($this->general_model->update_information($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}

	function add_article($arr)
	{
		if ($this->general_model->add_article($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']."/".$arr['is_company']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']."/".$arr['is_company']);
		}
		
	}
	
	function add_faq_item($arr)
	{
		if ($this->general_model->add_faq_item($arr,2))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
	function update_faq_item($arr)
	{
		if ($this->general_model->update_faq_item($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
	function add_review_item($arr)
	{
		if ($this->general_model->add_faq_item($arr,1))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
	function update_review_item($arr)
	{
		if ($this->general_model->update_faq_item($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}


	function update_article($arr,$fileimage)
	{
		
		if ($this->general_model->update_article($arr,$fileimage))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
	}
	function update_product($arr,$fileimage)
	{
		
		if ($this->general_model->update_product($arr,$fileimage))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
	}
	function com_support($arr) 
	{
		//echo "<br>".$arr['option']."/".$arr['task'];
			switch ($arr['option']) 
			{
				case "add_support":
					$this->add_support($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function add_support($arr)
	{
		$data['name1'] = xss_clean($this->input->post('supportname_vn'), $charset = 'UTF-8');
		$data['name2'] = xss_clean($this->input->post('supportname'), $charset = 'UTF-8');
		$data['nick'] = $this->input->post('nick');
		if ($this->general_model->add_support($data))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
	}

// USERS MANAGER
	function com_category($arr) 
	{
			switch ($arr['option']) 
			{
				case "add_category_item":
					$this->add_category_item($arr);
					break;
				case "add_job_category":
					$this->add_job_category($arr);
					break;
				case "add_program_category":
					$this->add_program_category($arr);
					break;
				case "add_country":
					$this->add_country($arr);
					break;
				case "add_postcode":
					$this->add_postcode($arr);
					break;	
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function com_link($arr) 
	{
		//echo "<br>".$arr['option']."/".$arr['task'];
			switch ($arr['option']) 
			{
				case "add_link":
					$this->add_link($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function add_link($arr)
	{
		if ($this->general_model->add_link($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
	function add_category_item($arr)
	{
		//var_dump ($arr); banner.swf
		if ($this->general_model->add_category_item($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
	function add_job_category($arr)
	{
		//var_dump ($arr);
		if ($this->general_model->add_job_category($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	function add_program_category($arr)
	{
		//var_dump ($arr);
		if ($this->general_model->add_program_category($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
	function add_country($arr)
	{
		//var_dump ($arr);
		if ($this->general_model->add_country($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}

	function add_postcode($arr)
	{
		//var_dump ($arr);
		if ($this->general_model->add_postcode($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}

	function add_menuproduct($arr)
	{
		
		$data['name1'] = xss_clean($this->input->post('menuname_vn'), $charset = 'UTF-8');
		$data['seo1'] =$this->seo_url->my_seo_url($data['name1']);
		$data['name2'] = xss_clean($this->input->post('menuname'), $charset = 'UTF-8');
		$data['seo2'] =$this->seo_url->my_seo_url($data['name2']);
		$data['sorting'] = $this->input->post('sort');
		
		//$this->user_model->add_menuproduct($data);
		if ($this->menu_model->add_menuproduct($data))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}
		
	}
	
// USERS MANAGER
	function com_users($arr) 
	{
		//echo "<br>".$arr['option']."/".$arr['task'];
			switch ($arr['option']) 
			{
				case "add_user":
					$this->add_user($arr);
					break;
				case "update_user":
					$this->update_user($arr);
					break;					
				case "delete_user":
					$this->delete_user($arr['id']);
					break;				
				case "changepassword":
					$this->change_password($arr['id'],$arr['newpassword'],$arr['myurl']);
					break;
				case "forgotten_password":
					$this->forgotten_password($arr);
					break;
				default:
					echo "You can't access this";
					break;
			}
		
	}
	function add_user()
	{
		$data['username'] = xss_clean($this->input->post('username'), $charset = 'ISO-8859-1');
		$data['password'] = md5($this->input->post('password').'dkw');
		$data['fullname'] = $this->input->post('fullname');
		$data['birthdate'] = $this->input->post('birthdate');
		$data['email'] = $this->input->post('email');
		$data['phone'] = $this->input->post('phone');
		$data['address'] = $this->input->post('address');
		$data['lastlogin'] = date("Y-m-d h:i:s");
		$data['ua_group_id'] = $this->input->post('group');	
		$data['active'] = 1; // if require email confirm --> value=0	
		
		//echo $data['birthdate']."/".$data['lastlogin'];	
		$this->user_model->insert_new_user($data);
	}
	function update_user()
	{

	}
	function delete_user()
	{

	}
	function change_password($user_ID,$newpassword,$myurl)
	{
		if ($this->user_model->change_password($user_ID,$newpassword))
		{
			unset($_SESSION['logged_in_status']);
	    	redirect('/admin/');
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="message">'.$this->lang->line('action_error').'</div>');
			redirect($myurl);
		}
	}
	function forgotten_password()
	{

	}
	
//-------------- END USERS MANAGER	-----------------------

// CONFIG MANAGER
	function com_config($arr) 
	{
		$this->load->helper('file');
		//$a=read_file('./readfile.txt');
		//$ss=get_filenames('./js/');
		//var_dump ($ss);
		//echo unserialize($a);
		$websitename=str_replace("'","&acute;",$arr['websitename']);
		$websitename=str_replace('"',"&quot;",$arr['websitename']);
		
		$keyword_site=str_replace("'","&acute;",$arr['keyword']);
		$keyword_site=str_replace('"',"&quot;",$arr['keyword']);

		$address=str_replace("'","&acute;",$arr['address']);
		$address=str_replace('"',"&quot;",$arr['address']);
		
		$data='<?php $websitename="'.$websitename.'"; $email="'.$arr['email'].'"; $phone="'.$arr['phone'].'"; $fax="'.$arr['fax'].'"; $address="'.$address.'"; $keyword_site="'.$keyword_site.'";';
		$result=write_file('./backend/globalconfig.php', $data,'w+');
		
		if ($result)
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($arr['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($arr['myurl']);
		}

	}
//--------------  END CONFIG MANAGER -------------- 	

}
